package com.lz.gmall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lz.gmall.common.result.Result;
import com.lz.gmall.common.result.ResultCodeEnum;
import com.lz.gmall.product.dto.SkuInfoDto;
import com.lz.gmall.product.entity.SkuInfo;
import com.lz.gmall.product.service.SkuInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
@Api(tags = "sku产品操作")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 获取sku分页列表
     * @param pageNum 页码
     * @param limit 每天条数
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    @ApiOperation("获取sku分页列表")
    public Result<Page<SkuInfo>> getSkuInfoByPage(@PathVariable("page")Long pageNum,@PathVariable("limit")Long limit){

        Page<SkuInfo> skuInfoList = skuInfoService.getSkuInfoByPage(pageNum,limit);
        return Result.build(skuInfoList, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加sku
     * @param skuInfoDto 封装了所有的sku参数
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfoDto skuInfoDto){
        skuInfoService.saveSkuInfo(skuInfoDto);
        return Result.ok();
    }

    /**
     * 修改skuInfo表中的is_sale字段进行上架操作，1表示上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    @ApiOperation("上架")
    public Result onSale(@PathVariable("skuId")Long skuId){
        skuInfoService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 修改skuInfo表中的is_sale字段进行下架操作，0表示上架
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    @ApiOperation("下架")
    public Result cancelSale(@PathVariable("skuId")Long skuId){
        skuInfoService.cancelSale(skuId);
        return Result.ok();
    }
}
